//
//  MainVideoInfoViewController.m
//  HorizonLight
//
//  Created by BaQi on 15/9/23.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "MainVideoInfoViewController.h"
#import "MainVideoInfoView.h"
#import "MediaViewController.h"
@interface MainVideoInfoViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) MainVideoInfoView *mainVideoInfoView;

//创建一个接收图片的数组
@property (nonatomic, strong) NSMutableArray *pictureArray;
@end

@implementation MainVideoInfoViewController
{
    UIAlertView *alertView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置navigationController的透明度
    self.navigationController.navigationBar.translucent = NO;
    //隐藏当前界面的UITanBar
    [self.tabBarController.tabBar setHidden:YES];

    //初始化UIView
    self.mainVideoInfoView = [[MainVideoInfoView alloc]initWithFrame:kBounds taget:self action:@selector(settingClick)section:self.section row:_row array:self.videoArray];
    VideoListModel *model = self.videoArray[_section][_row];
    [self.mainVideoInfoView setUpViewWithModel:model section:self.section array:self.videoArray];
    [self.mainVideoInfoView.clickPraiseButton addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.mainVideoInfoView.shareButton addTarget:self action:@selector(shareClick:) forControlEvents:(UIControlEventTouchUpInside)];
    self.mainVideoInfoView.scrollView.delegate = self;
    [self.view addSubview:self.mainVideoInfoView];

    
}
#pragma mark----开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"开始拖拽时1");
}


#pragma mark----拖拽结束时
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    NSLog(@"快要结束拖拽时2");
}


#pragma mark----拖拽结束时触发
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
   
    NSLog(@"拖拽时结束3");
}



#pragma mark----手指离开时
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"手指离开之前4");
}


#pragma mark----滑动图片结束时触发
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
     NSLog(@"========%@", NSStringFromCGPoint(scrollView.contentOffset));
    NSLog(@"滑动时触发5");
    self.row = scrollView.contentOffset.x / kScreenWidth;
    NSLog(@"%ld",self.row);
    if (self.row < [self.videoArray[self.section] count])
    {
        VideoListModel *model = self.videoArray[self.section][_row];
        [self.mainVideoInfoView changeContentValueWithModel:model];
    }
    else
    {
       self.section++;
        VideoListModel *model = self.videoArray[_section][0];
        [self.mainVideoInfoView setUpViewWithModel:model section:self.section array:self.videoArray];
        scrollView.contentOffset = CGPointMake(0, 0);
    }
    
}



#pragma mark----设置图片的点击方法
- (void)settingClick
{
    
    MediaViewController *mediaVC = [[MediaViewController alloc]init];
    VideoListModel *model = self.videoArray[_section][_row];
    mediaVC.mediaUrl = model.highUrl;
    mediaVC.headline = model.title;
    [self presentViewController:mediaVC animated:YES completion:nil];
}

#pragma mark----收藏按钮
- (void)click:(UIButton *)button
{
    button.selected = !button.selected;
    VideoListModel *model = self.videoArray[_section][_row];
    if (button.selected == YES)
    {
        //点击第一次时收藏成功
        [button setBackgroundImage:[UIImage imageNamed:@"FontAwesome_f004(0)_48"] forState:(UIControlStateSelected)];
        self.mainVideoInfoView.clickPraiseLabel.text = [NSString stringWithFormat:@"%lu",model.collectionCount + 1];
        //添加一个弹窗. 显示是否成功
        alertView = [[UIAlertView alloc]initWithTitle:@"已收藏成功" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
        //设置弹窗自动消失
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(view:) userInfo:nil repeats:NO];
        [alertView show];
    }else
    {
        //点击第二次时取消收藏
        [button setBackgroundImage:[UIImage imageNamed:@"FontAwesome_f08a(0)_48"] forState:(UIControlStateNormal)];
        self.mainVideoInfoView.clickPraiseLabel.text = [NSString stringWithFormat:@"%lu", model.collectionCount];
    }
}

#pragma mark----设置时间弹窗自动消失
- (void)view:(NSTimer *)timer
{
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
}


#pragma mark----分享按钮
- (void)shareClick:(UIButton *)button
{
    button.selected = !button.selected;
    VideoListModel *model = self.videoArray[_section][_row];
    if (button.selected == YES)
    {
        self.mainVideoInfoView.shareLabel.text = [NSString stringWithFormat:@"%lu", model.shareCount + 1];
    }else
    {
        self.mainVideoInfoView.shareLabel.text = [NSString stringWithFormat:@"%lu",model.shareCount];
    }
}




#pragma mark-----隐藏TabBar的方法
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self.hidesBottomBarWhenPushed = YES;
    return self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
